////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Base                                       //
////////////////////////////////////////////////
-- dhlite table is declared by the Cloud. Don't empty it on this file.

-- Don't use local shortcut here, it fails (index upvalue)
----local dhlite = dhlite

DHLITE_NAME = "DepthHUD Lite"
DHLITE_SHORT = "DHLITE"

DHLITE_DEBUG_SKINGENERATOR = math.random( 10000, 99999 )

dhlite_util = {}

function dhlite.IsEnabled()
	-- Here we use dhlite and no method form, as an extra security for external scripts
	-- that would try to call dhlite.IsEnabled()
	return dhlite.GetVar("dhlite_core_enable") > 0

end

function dhlite.ReloadFromCloud()
	if dhlite_cloud then
		dhlite_cloud:Ask()
	end
	
end

function dhlite.ReloadFromLocale()
	if dhlite_cloud then
		dhlite_cloud:LoadLocale()
	end
	
end

function dhlite.Mount()
	local self = dhlite
	
	dhlite_util.OutputLineBreak( )
	dhlite_util.OutputIn( "Mounting ..." )
	
	if dhlite_internal and DHLITE_FORCE_VERSION and not DHLITE_FORCE_USE_CLOUD then	
		dhlite_internal.QueryVersion( nil )
	end
	
	self.dat = {}
	
	self:RequireParameterMediator( )
	self:CreateVarParam( "bool", "core_enable", "1", { callback = function( sCvar, prev, new )
			if not dhlite then return end
			-- The following line is performed by the param already, as it is a bool.
			--if (tonumber( new ) <= 0 and tonumber( prev ) <= 0) or (tonumber( new ) > 0 and tonumber( prev ) > 0) then return end
			
			if tonumber( new ) > 0 then
				dhlite:MountCanvas()
			
			else
				dhlite:UnmountCanvas()
				
			end
			
		end } )
	self:CreateVarParam( "bool", "core_debug", "0" )
	self:CreateVarParam( "color", "uidesign_maincolor", {147, 201, 224, 255} )
	self:CreateVarParam( "color", "uidesign_backcolor", {32, 37, 43, 128} )
	
	self:CreateVarParam( "range", "editor_snap", 16 )
	
	
	self.cmdGroups = {}
	self.cmdGroups.call = {}
	self:AppendCmd( self.cmdGroups, "core_enable", function(p,c,args) self:SetVar("core_enable", args[1] ) end )
	
	
	self:AppendCmd( self.cmdGroups.call, "changelog", function() self.ShowChangelog( self ) end )
	self:AppendCmd( self.cmdGroups.call, "menu", function() self.OpenMenu( self ) end )
	self:AppendCmd( self.cmdGroups, "menu", function() self.OpenMenu( self ) end )
	self:AppendCmd( self.cmdGroups, "+menu", function() self.OpenMenu( self ) end )
	self:AppendCmd( self.cmdGroups, "-menu", function() self.CloseMenu( self ) end )
	
	self.cmdGroupsNoRemove = {}
	self:AppendCmd( self.cmdGroupsNoRemove, "cloud_locale", dhlite.ReloadFromLocale )
	
	self:BuildCmds( self.cmdGroups, "" )
	self:BuildCmds( self.cmdGroupsNoRemove, "" )
	
	self:MountCanvas()
	self:MountMenu()
	self:__DebugMakeElements()
	
	hook.Add("Think", "DHLITE_Think", dhlite.Think)
	hook.Add("HUDPaint", "DHLITE_HUDPaint", dhlite.HUDPaint)

	dhlite_util.OutputIn( "Mount complete : " .. (dhlite_internal.IsUsingCloud() and "Cloud" or "Locale") )
	dhlite_util.OutputLineBreak( )
	
end

function dhlite.Unmount()
	local self = dhlite

	local bOkay, strErr = pcall(function()
		-- Insert parachute Unmount
		
		dhlite_util.OutputLineBreak( )
		dhlite_util.OutputOut( "Unmounting ..." )
		
		hook.Remove("HUDPaint", "DHLITE_HUDPaint")
		hook.Remove("Think", "DHLITE_Think")
	
		self:DestroyChangelog()
		self:UnmountCanvas()
		self:UnmountMenu()
		self:DismountCmds( self.cmdGroups )
		
		
		dhlite_util.OutputOut( "Unmount complete." )
		dhlite_util.OutputLineBreak( )
		
	end)
	if not bOkay then
		dhlite_util.OutputError( tostring(strErr) , "while unmounting" )
		
	end
	
	-- Don't remove dhlite_util
	-- dhlite_util = {}
	dhlite = nil
	
end


